<?php

namespace app\result;

use app\model\admin\Notice;
use app\model\user\UserMsg;

class UserMsgResult extends BaseResult
{
    /**
     * 添加消息
     * @param $user_id int 接收者用户ID
     * @param $data array 消息内容 ["title","content","type",'path_info' => [
     * 'title' => '反馈列表',
     * 'path' => '/pages/setting/feedback',
     * 'jump_type' => 'inner',//inner=内页，tab=底部导航，link=外链
     * ]]
     * @return bool|int
     */
    public static function add($user_id, $data)
    {
        if (empty($user_id)) {
            return false;
        }
        if (empty($data)) {
            return false;
        }
        $baseData = [
            'user_id' => $user_id,
            'title' => $data['title'] ?? '',
            'content' => $data['content'] ?? "",
            'type' => $data['type'] ?? 1,
            'notice_id' => intval($data['notice_id'] ?? 0),
            'path_info' => !empty($data['path_info']) ? json_encode($data['path_info'], JSON_UNESCAPED_UNICODE) : '',
        ];

        return (new UserMsg())->data_add($baseData);
    }

    //消息列表
    public function list($user_id, $args = [])
    {
        $page = max($args['page'] ?? 1, 1);
        $page_size = max($args['page_size'] ?? 10, 1);
        $keyword = trim($args['keyword'] ?? '');
        $unread = intval($args['unread'] ?? 0);
        if ($page_size > 100) {
            $page_size = 100;
        }
        $where = [
            ['user_id', '=', $user_id],
        ];
        if (!empty($keyword) && strtolower($keyword) !== 'null') {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }
        if ($unread) {
            $where[] = ['read_time', '=', 0];
        }
        $model = new UserMsg();
        $total = $model->data_count($where);
        $data = $model->data_list($where, 'msg_id,title,type,created_at,content,read_time', 'msg_id desc', $page, $page_size);
        if ($data) {
            foreach ($data as &$v) {
                $v['content'] = mb_substr(strip_tags(htmlspecialchars_decode($v['content'])), 0, 35, 'utf-8') . '...';
            }
            unset($v);
        }
        $unread_count = $model->data_count(['user_id' => $user_id, 'read_time' => 0]);//未读消息数
        return self::success_return('success', [
            'total' => $total,
            'list' => $data,
            'unread_count' => $unread_count
        ]);
    }

    //全部设置为已读
    public function read_all($user_id)
    {
        $model = new UserMsg();
        $model->data_update(['user_id' => $user_id, 'read_time' => 0], ['read_time' => time()]);
        return self::success_return('success');
    }

    //消息详情
    public function get($user_id, $msg_id)
    {
        $model = new UserMsg();
        $data = $model->data_get(['user_id' => $user_id, 'msg_id' => $msg_id], 'msg_id,title,content,type,read_time,path_info');
        if (!$data) {
            return self::error_return('消息不存在');
        }
        $data = $data->toArray();
        $data['content'] = htmlspecialchars_decode($data['content']);
        //更新阅读时间
        if (empty($data['read_time'])) {
            $model->data_update(['msg_id' => $msg_id], ['read_time' => time()]);
        }
        if ($data['path_info']) {
            $data['path_info'] = json_decode($data['path_info'], true);
        }
        $data['read_time'] = $data['read_time'] ? date('Y-m-d H:i:00', $data['read_time']) : date('Y-m-d H:i:00');
        return self::success_return('success', $data);
    }
}
